package com.tzxs.pay.util;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;

import java.io.UnsupportedEncodingException;

public class PrintCommands {
    public static final byte ALIGNMENT_LEFT = 48;
    public static final byte ALIGNMENT_CENTER = 49;
    public static final byte ALIGNMENT_RIGHT = 50;
    private static final String TAG = PrintCommands.class.getName();

    public static byte[] bytesForPrintText(String text, int fontWidth, int fontHeight, byte alignment) {
        byte fontWidthByte, fontHeightByte;
        switch (fontWidth) {
            case 1:
                fontWidthByte = 0x00;
                break;
            case 2:
                fontWidthByte = 0x10;
                break;
            case 3:
                fontWidthByte = 0x20;
                break;
            case 4:
                fontWidthByte = 0x30;
                break;
            case 5:
                fontWidthByte = 0x40;
                break;
            case 6:
                fontWidthByte = 0x50;
                break;
            case 7:
                fontWidthByte = 0x60;
                break;
            default:
                fontWidthByte = 0x70;
        }
        switch (fontHeight) {
            case 1:
                fontHeightByte = 0x00;
                break;
            case 2:
                fontHeightByte = 0x01;
                break;
            case 3:
                fontHeightByte = 0x02;
                break;
            case 4:
                fontHeightByte = 0x03;
                break;
            case 5:
                fontHeightByte = 0x04;
                break;
            case 6:
                fontHeightByte = 0x05;
                break;
            case 7:
                fontHeightByte = 0x06;
                break;
            default:
                fontHeightByte = 0x07;
        }
        byte[] bytesForAlignment = {27, 97, alignment};
        byte[] bytesForFontSize = {29, 33, (byte) (fontWidthByte | fontHeightByte)};
        try {
            byte[] textBytes = text.getBytes("GBK");
            byte[] bytes = mergeArrays(bytesForAlignment, bytesForFontSize, textBytes);
            return bytes;
        } catch (UnsupportedEncodingException ex) {
            Log.e(TAG, null, ex);
        }
        return null;
    }

    public static byte[] bytesForPrintBitmap(Bitmap bitmap, byte alignment) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int pixelRed;
        int pixelGreen;
        int pixelBlue;
        int[] pixels = new int[width * height];
        int arrayWidth = ((width - 1) / 8) + 1;
        int arrayLength = arrayWidth * height;
        byte[] dataArray = new byte[arrayLength + 8];
        dataArray[0] = 0x1D;
        dataArray[1] = 0x76;
        dataArray[2] = 0x30;
        dataArray[3] = 0x00;
        dataArray[4] = (byte) arrayWidth;
        dataArray[5] = (byte) (arrayWidth / 256);
        dataArray[6] = (byte) height;
        dataArray[7] = (byte) (height / 256);
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
        int indexByte = 8;
        dataArray[indexByte] = 0;
        int indexBit = 0;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {//每一行进行转换，转换完成后，可能最后一个字节需要将数据移到高位
                //获取当前像素值的r部分
                pixelRed = Color.red(pixels[i * width + j]);
                //获取当前像素值的g部分
                pixelGreen = Color.green(pixels[i * width + j]);
                //获取当前像素值的b部分
                pixelBlue = Color.blue(pixels[i * width + j]);
                if ((pixelRed + pixelGreen + pixelBlue) < 384) {
                    dataArray[indexByte] += 0x01;
                }
                ++indexBit;
                if (indexBit < 8) {
                    dataArray[indexByte] *= 2;//相当于左移一位
                } else {
                    indexBit = 0;
                    ++indexByte;
                    if (indexByte < arrayLength) {
                        dataArray[indexByte] = 0;
                    }
                }
            }
            if (indexBit != 0) {
                while (indexBit < 8) {
                    dataArray[indexByte] *= 2;//相当于左移一位
                    ++indexBit;
                }
                indexBit = 0;
                ++indexByte;
                if (indexByte < arrayLength) {
                    dataArray[indexByte] = 0;
                }
            }
        }
        byte[] bytesForAlignment = {27, 97, alignment};
        byte[] bytes = mergeArrays(bytesForAlignment, dataArray);
        return bytes;
    }

    public static byte[] bytesForPrintQRCode(String text, int unitSize, byte alignment) {
        byte[] bytesForAlignment = {27, 97, alignment};
        byte[] bytesForQRCodeUnitSize = {29, 40, 107, 3, 0, 49, 67, (byte) unitSize};
        byte[] bytesForStoreQRCodeData = new byte[]{29, 40, 107, 0, 0, 49, 80, 48};
        byte[] qrcodeDataBytes = text.getBytes();
        int cmdDataLen = qrcodeDataBytes.length + 3;
        bytesForStoreQRCodeData[3] = (byte) (cmdDataLen % 256);
        bytesForStoreQRCodeData[4] = (byte) (cmdDataLen / 256);
        byte[] bytesForPrintQRCode = {29, 40, 107, 3, 0, 49, 81, 48};
        byte[] bytes = mergeArrays(bytesForAlignment, bytesForQRCodeUnitSize, bytesForStoreQRCodeData, qrcodeDataBytes, bytesForPrintQRCode);
        return bytes;
    }

    public static byte[] bytesForCutPaper() {
        byte[] cutPaperCommand = {0x1d, 0x56, 0x42, 0x00};
        return cutPaperCommand;
    }

    private static byte[] mergeArrays(byte[]... arrays) {
        int lengthOfNewArray = 0;
        for (int i = 0; i < arrays.length; ++i) {
            lengthOfNewArray += arrays[i].length;
        }
        byte[] newArray = new byte[lengthOfNewArray];
        int destPos = 0;
        for (int i = 0; i < arrays.length; ++i) {
            System.arraycopy(arrays[i], 0, newArray, destPos, arrays[i].length);
            destPos += arrays[i].length;
        }
        return newArray;
    }
}
